<?php
/************************************
*   新浪相册 7.0
*   @file:api_controllor.php
*   @intro:接口控制器
*   @author:shaopu@
*   @date:Sun Sep 20 17:35:03 CST 2009
************************************/


class api_controllor extends controllor 
{
    public function __construct($get_param)
    {
        parent::controllor($get_param);
        parse_str($get_param[0] , $this->__get_param);
        
        if($this->__need_session)
        {
            $this->init_session();
            
            if($this->__need_login && !$this->login)
                $this->err_output(K_SYSCODE_NOT_LOGIN);
        }
        
        $this->_start_time = K::microtime();
        
        K::set_controllor($this);
        
        
        $this->construct();
        $this->run();
    }

    /**
     * 输出
     *
     * @param array $data
     */
    private function _output($data)
    {
        $format = strtoupper($this->input('format'));
        $domain = $this->get_input('domain') == 1 ? true : false;
        $jsonc = $this->get_input('jsonc') == 1 ? true : false;
        
		if (!in_array($format, array('JSON', 'JSONP', 'XML', 'PHP')))
		{
			$format = 'JSON';
		}
    	if ('JSON' == $format || 'JSONP' == $format)
    	{
    		if ($domain)
    		{
    			echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . "\n";
    			echo '<script type="text/javascript">document.domain="'.K5_ROOT_DOMAIN.'";</script>' . "\n";
    		}
    		$varname = $this->get_input('varname');
			$jsonp = $this->get_input('jsonp');
    		if ('' != $varname)
			{
				echo $varname . '=' . json_encode($data);
			}
			elseif(preg_match('/^[0-9a-zA-Z-_]{1,}$/' , $jsonp))
			{
				echo $jsonp . '(' . json_encode($data) . ')';
			}
			else
			{
			    if($jsonc)
			        echo '<textarea id="bsIfrmData">'.json_encode($data).'</textarea>';
			    else
			    {
				    echo json_encode($data);
			    }
			}
    	}
		elseif ('XML' == $format)
		{
			echo K_str::xml_encode($data);
		}
		elseif('PHP' == $format) 
		{
			echo serialize($data);
		}
		   
		$this->_over();
		
		exit;
    }
    /**
     * 成功输出
     *
     * @param mix $mData
     */
    protected function suc_output($mData = array())
    {
        $aData = array(
            'code' => K_SYSCODE_OK,
        );
        
        if(!empty($mData))
            $aData['data'] = $mData;
            
            
        $this->_output($aData);
    }
    /**
     * 错误输出
     *
     * @param string $code
     * @param mix $mData
     */
    protected function err_output($code , $mData = array())
    {
        $aData = array(
            'code' => $code,
        );
        if(!empty($mData))
            $aData['data'] = $mData;
        $this->_output($aData);
    }
    /**
     * 系统繁忙
     *
     */
    protected function busy($msg = '')
    {
        $this->err_output(K_SYSCODE_SYSTEM_BUSY, array('comment'=>$msg));
    }
    
    //~~以下程序需要在子类中被覆盖~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /**
     * 程序执行入口
     *
     */
    public function run(){}
}

?>